﻿using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using CodeSnippet.Properties;

namespace CodeSnippet.Forms
{
    /// <summary>
    /// About dialog for the plugin.
    /// </summary>
    internal partial class CodeSnippetAboutForm : Form
    {
        #region Properties
        /// <summary>
        /// Gets the assembly title.
        /// </summary>
        /// <value>The assembly title.</value>
        public static string AssemblyTitle
        {
            get
            {
                var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof (AssemblyTitleAttribute), false);
                if (attributes.Length > 0)
                {
                    var titleAttribute = (AssemblyTitleAttribute) attributes[0];
                    if (!string.IsNullOrEmpty(titleAttribute.Title))
                    {
                        return titleAttribute.Title;
                    }
                }
                return Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
            }
        }
        /// <summary>
        /// Gets the assembly version.
        /// </summary>
        /// <value>The assembly version.</value>
        public static string AssemblyVersion
        {
            get { return Assembly.GetExecutingAssembly().GetName().Version.ToString(); }
        }
        /// <summary>
        /// Gets the assembly description.
        /// </summary>
        /// <value>The assembly description.</value>
        public static string AssemblyDescription
        {
            get
            {
                var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
                return attributes.Length == 0 ? string.Empty : ((AssemblyDescriptionAttribute)attributes[0]).Description;
            }
        }
        /// <summary>
        /// Gets the assembly product.
        /// </summary>
        /// <value>The assembly product.</value>
        public static string AssemblyProduct
        {
            get
            {
                var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyProductAttribute), false);
                return attributes.Length == 0 ? string.Empty : ((AssemblyProductAttribute)attributes[0]).Product;
            }
        }
        /// <summary>
        /// Gets the assembly copyright.
        /// </summary>
        /// <value>The assembly copyright.</value>
        public static string AssemblyCopyright
        {
            get
            {
                var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
                return attributes.Length == 0 ? string.Empty : ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
            }
        }
        /// <summary>
        /// Gets the assembly company.
        /// </summary>
        /// <value>The assembly company.</value>
        public static string AssemblyCompany
        {
            get
            {
                var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
                return attributes.Length == 0 ? string.Empty : ((AssemblyCompanyAttribute)attributes[0]).Company;
            }
        }
        #endregion Properties

        #region Initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="CodeSnippetAboutForm"/> class.
        /// </summary>
        public CodeSnippetAboutForm()
        {
            InitializeComponent();
            InitializeChildren();
        }
        /// <summary>
        /// Initializes the children.
        /// </summary>
        private void InitializeChildren()
        {
            Text = string.Format(CultureInfo.CurrentUICulture, Resources.FormAbout_Title, AssemblyTitle);
            tbProductName.Text = AssemblyProduct;
            tbVersion.Text = string.Format(CultureInfo.CurrentUICulture, Resources.FormAbout_Version, AssemblyVersion);
            tbCopyright.Text = AssemblyCopyright;
            rtbCompanyName.Text = string.Format(CultureInfo.CurrentUICulture, Resources.FormAbout_Company, AssemblyCompany);
            tbDescription.Text = AssemblyDescription;
        }
        #endregion Initialization

        #region Event Handlers
        /// <summary>
        /// Handles the LinkClicked event of the rtbCompanyName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.LinkClickedEventArgs"/> instance containing the event data.</param>
        private void rtbCompanyName_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            // Call Process.Start method to open the default browser with link text as URL.
            Process.Start(e.LinkText);
            // Put the focus back on the default button.
            btnOK.Focus();
        }
        #endregion Event Handlers
    }
}